package cn.xu.tool.big.a.compments;

import cn.xu.tool.big.a.dto.KLine;
import cn.xu.tool.big.a.dto.TotalReturn;
import cn.xu.tool.big.a.dto.TradeRecord;
import cn.xu.tool.big.a.util.FileUtil;
import com.alibaba.fastjson.JSON;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 类名: TextReport
 * 描述: TODO
 * 作者: ClarkXu clark2021@qq.com
 * 日期: 2025/7/14
 */
public class TextReport {
    private static final String PARENT_PATH = "D:\\work\\code\\clark\\gitee\\big_a\\datas";
    public static void saveTextReport(List<KLine> kLines, List<TradeRecord> tradeRecords,
                                      String stockCode, String startDate, String endDate, TotalReturn totalReturn) throws IOException {
        String dirPath = PARENT_PATH + "/backtest/"+stockCode;
        File dir = new File(dirPath);
        if(!dir.exists()){
            dir.mkdir();
        }
        String outFile = dirPath+"/"+startDate+"-"+endDate+"_chart.txt";
        File old = new File(outFile);
        if(old.exists()){
            old.delete();
        }

        String title = "资产与价格走势分析 (" + startDate + "-" + endDate + ")";
        FileUtil.writeAppend(title, outFile);

        StringBuilder sb = new StringBuilder();
        sb.append("初始资金: ￥").append(totalReturn.getInitialCapital()).append(" ");
        sb.append("最终资金: ￥").append(totalReturn.getFinalCapital()).append(" ");
        sb.append("总收益率: ").append(totalReturn.getReturnRate()).append(" ");
        sb.append("交易次数: ").append(totalReturn.getTradeCount());

        FileUtil.writeAppend(sb.toString(), outFile);

        FileUtil.writeAppend("交易K线数据:", outFile);
        for (KLine kLine:kLines){
            FileUtil.writeAppend(JSON.toJSONString(kLine), outFile);
        }

        FileUtil.writeAppend("买卖操作数据:", outFile);
        for (TradeRecord tradeRecord:tradeRecords){
            FileUtil.writeAppend(JSON.toJSONString(tradeRecord), outFile);
        }
    }
    public static void saveTextReportPath(String parentPath, List<KLine> kLines, List<TradeRecord> tradeRecords,
                                      String stockCode, String startDate, String endDate, TotalReturn totalReturn) throws IOException {
        String dirPath = parentPath + "/backtest/"+stockCode;
        File dir = new File(dirPath);
        if(!dir.exists()){
            dir.mkdir();
        }
        String outFile = dirPath+"/"+startDate+"-"+endDate+"_chart.txt";
        File old = new File(outFile);
        if(old.exists()){
            old.delete();
        }

        String title = "资产与价格走势分析 (" + startDate + "-" + endDate + ")";
        FileUtil.writeAppend(title, outFile);

        StringBuilder sb = new StringBuilder();
        sb.append("初始资金: ￥").append(totalReturn.getInitialCapital()).append(" ");
        sb.append("最终资金: ￥").append(totalReturn.getFinalCapital()).append(" ");
        sb.append("总收益率: ").append(totalReturn.getReturnRate()).append(" ");
        sb.append("交易次数: ").append(totalReturn.getTradeCount());

        FileUtil.writeAppend(sb.toString(), outFile);

        FileUtil.writeAppend("交易K线数据:", outFile);
        for (KLine kLine:kLines){
            FileUtil.writeAppend(JSON.toJSONString(kLine), outFile);
        }

        FileUtil.writeAppend("买卖操作数据:", outFile);
        for (TradeRecord tradeRecord:tradeRecords){
            FileUtil.writeAppend(JSON.toJSONString(tradeRecord), outFile);
        }
    }
}
